package com.sam.report;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.Calendar.Events;
import com.google.api.services.calendar.model.Event;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;
import com.sam.entities.Admisores;
import com.sam.entities.ReporteCallUsers;
import com.sam.utils.Utils;

public class ReporteCall extends ActionSupport  {

	private static final long serialVersionUID = 6156699934149027950L;
	private List<ReporteCallUsers> result = new ArrayList<ReporteCallUsers>();
	
	public String execute() {
		try {

			
			Calendar service = Utils.getCalendarServices();
			
			
			
			
			Map<String, Integer> usuariosConEvento = new HashMap<String, Integer>( );
			
			 String uploadDir = ServletActionContext.getServletContext().getRealPath("/files/" + Utils.getProperty("admisoresFile"));
			 BufferedReader br = new BufferedReader(new FileReader(uploadDir));
			Gson gson = new Gson();
			Admisores obj = gson.fromJson(br, Admisores.class);
			for (int i = 0; i < obj.getAdmisor().size(); i++) {
				Events.List events = service.events().list("primary");
				events.getUnknownKeys().put("xoauth_requestor_id", obj.getAdmisor().get(i).getEmail());
				events.getUnknownKeys().put("key", Utils.GOOGLE_KEY);
				com.google.api.services.calendar.model.Events eventss = events.execute();
				boolean fin=false;
				while (!fin) {
					if(eventss != null && eventss.getItems() != null){
					  for (Event event : eventss.getItems()) {
						  //System.out.println("event.getSummary() = '"+event.getSummary()+"'  event.getExtendedProperties() = '"+event.getExtendedProperties()+"'");
							if (event.getExtendedProperties() != null)
							{
								
								if(event.getExtendedProperties().getShared().get("map_usuarioCreador") != null)
								{
									int count = usuariosConEvento.containsKey(event.getExtendedProperties().getShared().get("map_usuarioCreador")) ? usuariosConEvento.get(event.getExtendedProperties().getShared().get("map_usuarioCreador")) : 0;
									usuariosConEvento.put(event.getExtendedProperties().getShared().get("map_usuarioCreador"), count + 1);
								}
							}
					  }
					  String pageToken = eventss.getNextPageToken();
					  //System.out.println("pageToken = '"+pageToken+"'");
					  if (pageToken != null && !pageToken.isEmpty()) {
					    events = service.events().list("primary").setPageToken(pageToken);
						events.getUnknownKeys().put("xoauth_requestor_id", obj.getAdmisor().get(i).getEmail());
						events.getUnknownKeys().put("key", Utils.GOOGLE_KEY);
						eventss = events.execute();
						
						
					  } else {
						  fin=true;
					  }
					}
					else
					{fin=true;}
				}

			}
			
			 for (Map.Entry<String, Integer> entry : usuariosConEvento.entrySet()) {
				 result.add(new ReporteCallUsers(entry.getKey(), entry.getValue()));
				 //System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
		     }
			
			
			if(result.isEmpty())
			{
				addActionError(getText("No hay resultados para el reporte!"));
				  return ERROR;
			}
			
			
		
			
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(getText("Error al crear el Reporte!"));
			  return ERROR;
		}
		return SUCCESS;
	}
	
	
	public List<ReporteCallUsers> getResult() {
		return result;
	}

	public void setResult(List<ReporteCallUsers> result) {
		this.result = result;
	}


}
